//#pragma once
#include"graphics.h"

//**************************************棋子

extern WINDOW* g_win;
extern GRAPHICS_COLOR g_fore_color;
extern GRAPHICS_COLOR g_back_color;
extern int g_style;
extern POINT cursor;

extern int count;	                    	//全局变量存了多少棋子,用来判断颜色

void addChess(POINT cursorm,POINT* head)	//增加棋子
{
	POINT* p = NULL;	                	//临时指针
	POINT* tmp =NULL;		
	p = (POINT* )malloc(sizeof(POINT));
	
	p->x = cursorm.x;		                //初始化
	p->y = cursorm.y;
       p->next = NULL;
       
        //test for extern cursor 
        cursor.x=cursorm.x;
        cursor.y=cursorm.y;

	

	if(head->next == NULL)	               //判断栈是否为空
	{

		head->next = p;
		count++;		                   //棋子总数加1	
	}
	else
	{
		int i;
		bool flag = true;
		
		for(tmp = head->next,i=0; i < count; i++,tmp = tmp->next)	
		{		
			if( tmp->x == p->x && tmp->y == p->y ) //判断棋子坐标是否一样
			{
				flag = false;
				break;
			}
			
		}
	
		if(flag)			            	//棋子坐标全部不一样，就给p加入棋子栈列
		{
			p->next = head->next;
			head->next = p;
			count++;			

			
		}
		else 
		{
			free(p);
		}
	}		
}


void delChess(POINT* head)	              //减少棋子
{
	POINT* tmp = head->next;
	if(tmp == NULL)
		return;
	else
	{	
		tmp = tmp->next;
		free(head->next);
		head->next = tmp;
		count--;
	}
}


void drawChess(POINT* head)
{
	int tmp = count;	            	//临时变量判断颜色
	POINT* p = head;
	
	while(1)
	{
		if(p->next == NULL)	            //判断栈是否为空
			break;
		else
		{
			g_back_color = GCCyan;		//设置背景色
			if(tmp%2 != 0)
				g_fore_color=GCWhite;	//设置白棋前景色
			else
				g_fore_color=GCBlack;	//设置黑棋前景色
			wattrset(g_win,CURRENT_ATTRIBUTE);	//更新前景色和背景色
			mvaddstr( (p->next)-> x, (p->next)-> y, CHESS);
			
		}	
		p = p->next;					//指向下一个栈位
		tmp++;
	}

}